home *** CD-ROM | disk | FTP | other *** search
/ Java Primer Plus / Java Primer Plus (Waite Group Proess)(1996).iso / chapter15 / Netc.java < prev    next >
Text File  |  1995-12-31  |  2KB  |  128 lines

  1. import java.net.*;
  2. import java.io.*;
  3. import java.applet.*;
  4. import java.awt.*;
  5. import java.util.Random;
  6.  
  7. /* Network Client */
  8. /* YOU NEED TO REPLACE THE SERVER STRING WITH YOUR MACHINE (BELOW) */
  9.  
  10. public class Netc extends Applet implements Runnable {
  11.     
  12.     Socket sock;
  13.     DataInputStream datain;
  14.     DataOutputStream dataout;
  15.     Thread mythread = null;
  16.     Point All[] = new Point[10];
  17.     Point pos = new Point(254,254);
  18.     int num;
  19.     
  20.  
  21.     /* Applet initialization */
  22.     public void init() {
  23.  
  24.       resize(250,250);
  25.     
  26.        for (int g=0;g<10;++g) All[g] = new Point(-1,-1);
  27.      }
  28.  
  29.     /* Applet start */
  30.     public void start() {
  31.  
  32.  
  33.     /* Open socket to the server and setup the streams */
  34.       try {
  35.            InetAddress addr = InetAddress.getByName("serval.cat.syr.edu");
  36.            sock = new Socket(addr,1237);
  37.            datain = new 
  38.          DataInputStream(new BufferedInputStream(sock.getInputStream()));
  39.            dataout = new 
  40.          DataOutputStream(new BufferedOutputStream(sock.getOutputStream()));
  41.           } catch (IOException E) {}
  42.  
  43.  
  44.        if (mythread == null) {
  45.         mythread = new Thread(this);
  46.         mythread.start();
  47.         }
  48.          }
  49.  
  50.     /* Applet stop */
  51.     public void stop() {
  52.        if (mythread != null) {
  53.         mythread.stop();
  54.         mythread=null;
  55.  
  56.      /* additional cleanup (closings) */
  57.        try {
  58.          dataout.close();
  59.          datain.close();
  60.          sock.close();
  61.          } catch (IOException E);
  62.         }
  63.        }
  64.  
  65.  
  66.     /* The Thread's run method */
  67.     public void run() {
  68.       int tx,ty;
  69.  
  70.       while (true) {
  71.         try {
  72.         synchronized (pos) {
  73.          dataout.write(pos.x);
  74.          dataout.write(pos.y);
  75.          pos.x = pos.y = 0;    
  76.          }
  77.          dataout.flush();
  78.          tx = num = 0;
  79.  
  80.         do {   
  81.         tx = datain.read();
  82.         ty = datain.read();
  83.         All[num].move(tx,ty);
  84.         num++;
  85.         } while (tx != 255);
  86.  
  87.        } catch (IOException e) {}
  88.              catch (NullPointerException e) {
  89.         throw new NullPointerException("ACK!");
  90.         }
  91.  
  92.        repaint();
  93.        mythread.yield();
  94.        }
  95.       }
  96.  
  97.     public boolean mouseDrag(Event E, int x, int y) {
  98.      synchronized(pos) {
  99.        pos.x = x;
  100.        pos.y = y;
  101.        }
  102.        return true;
  103.        }
  104.     public boolean mouseDown(Event E, int x, int y) {
  105.        mouseDrag(E,x,y);
  106.        return true;
  107.        }
  108.     
  109.     public void update(Graphics G) {
  110.         paint(G);
  111.         }
  112.  
  113.  
  114.     public void paint(Graphics G) {
  115.         int g = 0;
  116.  
  117.         G.setColor(Color.red);        
  118.         while (All[g].x != -1) {
  119.           G.drawRect(All[g].x,All[g].y,1,1);
  120.           g++;
  121.           }
  122.         }
  123.  
  124.      }
  125.  
  126.     
  127.  
  128.